package ui.client;

import java.awt.Color;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import tool.LongStringTransformer;
import tool.MyButton;
import vo.ClientVo;
import vo.UserVo;
import vo.Vo;
import blservice.client.ClientBLService;

import javax.swing.JScrollPane;
import javax.swing.JTable;

import o.客户类型;
import client.boot.BLServiceManager;

public class ClientManagementPanel extends JPanel{
	
	
	private static final long serialVersionUID = 207744777412968236L;
	ClientBLService clientBL;
	List<ClientVo> clients;
	UserVo user;
	private JTextField textField_find;
	private JTable table;
	private JScrollPane jsp;
	private Vector<String> columnNames;
	
	public ClientManagementPanel(UserVo user){
		this.setBackground(new Color(0, 0, 0, 0));
		try {
			clientBL=BLServiceManager.getClientBLService(null);
		} catch (RemoteException | MalformedURLException | NotBoundException
				| SQLException e1) {
			JOptionPane.showMessageDialog(this,"无法连接到服务器"+e1.getMessage());
			e1.printStackTrace();
			return;
		}
		
		this.setLayout(null);
		this.user=user;
		
		columnNames = new Vector<String>();
		
		columnNames.add("编号");
		columnNames.add("分类");
		columnNames.add("级别");
		columnNames.add("姓名");
		columnNames.add("电话");
		columnNames.add("地址");
		columnNames.add("邮编");
		columnNames.add("电子邮箱");
		columnNames.add("应收额度");
		columnNames.add("应收");
		columnNames.add("应付");
		columnNames.add("默认业务员");
		
				
		
		
		JButton button_add =new MyButton("Graphics/Button93-23.png");
		button_add.setText("添加");
		button_add.setBounds(92, 382, 93, 23);
		add(button_add);
		button_add.addActionListener(e->addClient(user));
		
		textField_find = new JTextField();
		textField_find.setBounds(92, 17, 340, 21);
		add(textField_find);
		textField_find.setColumns(10);
		
		JButton button_find = new MyButton("Graphics/Button76-23.png");
		button_find.setText("查找");
		button_find.setBounds(469, 16, 76, 23);
		add(button_find);
		button_find.addActionListener(e->findClient(user));
		
		JButton button_update = new MyButton("Graphics/Button93-23.png");
		button_update.setText("修改");
		button_update.setBounds(452, 382, 93, 23);
		add(button_update);
		button_update.addActionListener(e->updateClient(user));
		
		
		JButton button_delete = new MyButton("Graphics/Button93-23.png");
		button_delete.setText("删除");
		button_delete.setBounds(270, 382, 93, 23);
		add(button_delete);
		button_delete.addActionListener(e->deleteClient(user));
		
		table = new JTable();
		jsp=new JScrollPane(table);
		jsp.setBounds(92, 75, 453, 273);
		this.add(jsp);
		
		showAll();
		this.setVisible(true);
	}

	private void addClient(UserVo user){
		try {
			int row = table.getSelectedRow();
			String classify = (String)table.getValueAt(row, 1);
			if(!(classify.equals("进货商")||classify.equals("销售商")))
				JOptionPane.showMessageDialog(this, "客户类型错误");
			int level = 0;
			try{
				level = Integer.parseInt((String)table.getValueAt(row, 2));
			}catch(NumberFormatException e){
				JOptionPane.showMessageDialog(this, "客户级别格式错误\n"+e.getMessage());
				e.printStackTrace();
			}
			String name = (String)table.getValueAt(row, 3);
			String tel = (String)table.getValueAt(row, 4);
			String address = (String)table.getValueAt(row, 5);
			String postcode = (String)table.getValueAt(row, 6);
			String mail = (String)table.getValueAt(row, 7);
			long receivable = 0;
			try{
			receivable = LongStringTransformer.StringToLong((String)table.getValueAt(row, 8));
			}catch(NumberFormatException e){
				JOptionPane.showMessageDialog(this, "应收额度格式错误\n");
				return;
			}
			
			ClientVo addVo = new ClientVo();
			addVo.setOperator(user.getName());
			addVo.setOperation("添加客户");
			addVo.set地址(address);
			addVo.set姓名(name);
			addVo.set邮编(postcode);
			addVo.set应付(0);
			addVo.set应收(0);
			addVo.set应收额度(receivable);
			addVo.set电子邮箱(mail);
			addVo.set电话(tel);
			if(classify.equals("进货商"))
				addVo.set类型(客户类型.进货商);
			if(classify.equals("销售商"))
				addVo.set类型(客户类型.销售商);
			addVo.set级别(level);
			addVo.set默认业务员(user.getName());
			
			Vo vo = clientBL.addClient(addVo);
			if(vo.isSuccessed()){
				JOptionPane.showMessageDialog(this, "添加成功\n");
				showAll();
			}
			else{
				JOptionPane.showMessageDialog(this, "添加失败\n"+vo.getFailMessage());
			}
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n"+e.getMessage());
			e.printStackTrace();
		}
	}

	private void deleteClient(UserVo user){
		try {
			int row = table.getSelectedRow();
			
			String num = (String)table.getValueAt(row, 0);
			String name = (String)table.getValueAt(row, 3);
			
			ClientVo deleteVo = new ClientVo();
			deleteVo.set编号(num);
			deleteVo.set姓名(name);
			
			Vo vo = clientBL.deleteClient(deleteVo);
			
			if(vo.isSuccessed()){
				JOptionPane.showMessageDialog(this, "删除成功\n");
				showAll();
			}
			else{
				JOptionPane.showMessageDialog(this, "删除失败"+vo.getFailMessage());
			}
			
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器"+e.getMessage());
			e.printStackTrace();
		}
	}
	
	private void updateClient(UserVo user){
		try {
			int row = table.getSelectedRow();
			String num = (String)table.getValueAt(row, 0);
			String classify = (String)table.getValueAt(row, 1);
			if(!(classify.equals("进货商")||classify.equals("销售商")))
				JOptionPane.showMessageDialog(this, "客户类型错误");
			int level = 0;
			try{
				level = Integer.parseInt((String)table.getValueAt(row, 2));
			}catch(NumberFormatException e){
				JOptionPane.showMessageDialog(this, "客户级别格式错误\n"+e.getMessage());
				e.printStackTrace();
			}
			String name = (String)table.getValueAt(row, 3);
			String tel = (String)table.getValueAt(row, 4);
			String address = (String)table.getValueAt(row, 5);
			String postcode = (String)table.getValueAt(row, 6);
			String mail = (String)table.getValueAt(row, 7);
			long receivable = 0;
			try{
			receivable = LongStringTransformer.StringToLong((String)table.getValueAt(row, 8));
			}catch(NumberFormatException e){
				JOptionPane.showMessageDialog(this, "应收额度格式错误\n");
				return;
			}
			
			
			ClientVo updateVo = new ClientVo();
			updateVo.setOperator(user.getName());
			updateVo.setOperation("修改客户");
			updateVo.set地址(address);
			updateVo.set姓名(name);
			updateVo.set应收额度(receivable);
			updateVo.set电子邮箱(mail);
			updateVo.set电话(tel);
			if(classify.equals("进货商"))
				updateVo.set类型(客户类型.进货商);
			if(classify.equals("销售商"))
				updateVo.set类型(客户类型.销售商);
			updateVo.set级别(level);
			updateVo.set默认业务员(user.getName());
			updateVo.set邮编(postcode);
			updateVo.set编号(num);
			
			Vo vo = clientBL.updateClient(updateVo);
			if(vo.isSuccessed()){
				JOptionPane.showMessageDialog(this, "修改成功\n");
				showAll();
			}
			else{
				JOptionPane.showMessageDialog(this, "修改失败\n"+vo.getFailMessage());
			}
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n"+e.getMessage());
			e.printStackTrace();
		}

	}
	
	private void findClient(UserVo user){
		try {
			ClientVo findVo = new ClientVo();
			Object keyword = textField_find.getText();
			findVo.setOperator(user.getName());
			findVo.setOperation("查找客户");
			findVo.set姓名((String)keyword);
			findVo.set地址((String)keyword);
			findVo.set电话((String)keyword);
			findVo.set编号((String)keyword);
			
			Vector<Vector<String>>rowData = new Vector<Vector<String>>();
			Vector<String> clientInfo;
			List<ClientVo> clientItem = clientBL.findClient(findVo);
			for(int i=0;i<clientItem.size();i++){
				clientInfo = new Vector<String>();
				clientInfo.add(clientItem.get(i).get编号());
				clientInfo.add(String.valueOf(clientItem.get(i).get类型()));
				clientInfo.add(Integer.toString(clientItem.get(i).get级别()));
				clientInfo.add(clientItem.get(i).get姓名());
				clientInfo.add(clientItem.get(i).get电话());
				clientInfo.add(clientItem.get(i).get地址());
				clientInfo.add(clientItem.get(i).get邮编());
				clientInfo.add(clientItem.get(i).get电子邮箱());
				clientInfo.add(LongStringTransformer.LongToString(clientItem.get(i).get应收额度()));
				clientInfo.add(LongStringTransformer.LongToString(clientItem.get(i).get应收()));
				clientInfo.add(LongStringTransformer.LongToString(clientItem.get(i).get应付()));
				clientInfo.add(clientItem.get(i).get默认业务员());
				rowData.add(clientInfo);
			}
			clientInfo = new Vector<String>();
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			rowData.add(clientInfo);
			if(table!=null){
				table.setVisible(false);
			}
			table = new JTable(rowData,columnNames){
				private static final long serialVersionUID = -1556317799866179607L;

				public boolean isCellEditable(int row,int column){
					if(column==0||column==9||column==10||column==11){
						return false;
					}
					else{
						return true;
					}
				}
			};
			table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(new JComboBox<String>(new String[]{"进货商","销售商"})));
			table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(new JComboBox<String>(new String[]{"0","1","2","3","4"})));
			if(jsp!=null){
				jsp.setVisible(false);
			}
			jsp = new JScrollPane(table);
			jsp.setBounds(0,51,686,319);
			add(jsp);
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
	}
	
	void showAll(){
		try {
			Vector<Vector<String>> rowData = new Vector<Vector<String>>();
			Vector<String>clientInfo;
			List<ClientVo> clientItem = clientBL.getAll();
			for(int i=0;i<clientItem.size();i++){
				clientInfo = new Vector<String>();
				clientInfo.add(clientItem.get(i).get编号());
				clientInfo.add(String.valueOf(clientItem.get(i).get类型()));
				clientInfo.add(String.valueOf(clientItem.get(i).get级别()));
				clientInfo.add(clientItem.get(i).get姓名());
				clientInfo.add(clientItem.get(i).get电话());
				clientInfo.add(clientItem.get(i).get地址());
				clientInfo.add(clientItem.get(i).get邮编());
				clientInfo.add(clientItem.get(i).get电子邮箱());
				clientInfo.add(tool.LongStringTransformer.LongToString(clientItem.get(i).get应收额度()));
				clientInfo.add(tool.LongStringTransformer.LongToString(clientItem.get(i).get应收()));
				clientInfo.add(tool.LongStringTransformer.LongToString(clientItem.get(i).get应付()));
				clientInfo.add(clientItem.get(i).get默认业务员());
				rowData.add(clientInfo);
			}
			clientInfo = new Vector<String>();
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			clientInfo.add("");
			rowData.add(clientInfo);
			if(table!=null){
				table.setVisible(false);
			}
			table = new JTable(rowData,columnNames){
				
				private static final long serialVersionUID = -8703388507573828328L;

				public boolean isCellEditable(int row,int column){
					if(column==0||column==9||column==10||column==11){
						return false;
					}
					else{
						return true;
					}
				}
			};
			table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(new JComboBox<String>(new String[]{"进货商","销售商"})));
			table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(new JComboBox<String>(new String[]{"0","1","2","3","4"})));
			if(jsp!=null){
				jsp.setVisible(false);
			}
			jsp = new JScrollPane(table);
			jsp.setBounds(0,51,650,319);
			add(jsp);
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
		
	}
	
}